package com.atguigu.upp.service;

import com.atguigu.upp.bean.TagInfo;
import com.atguigu.upp.bean.TaskInfo;
import com.atguigu.upp.bean.TaskTagRule;
import com.atguigu.upp.mapper.TagInfoMapper;
import com.atguigu.upp.mapper.TaskInfoMapper;
import com.atguigu.upp.mapper.TaskTagRuleMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

/**
 * Created by Smexy on 2023/1/3
 */
public class MysqlDBService
{
    private TagInfoMapper tagInfoMapper;
    private TaskInfoMapper taskInfoMapper;
    private TaskTagRuleMapper taskTagRuleMapper;

    //在构造器中为属性赋值
    public MysqlDBService(SqlSession sqlSession){
        this.tagInfoMapper = sqlSession.getMapper(TagInfoMapper.class);
        this.taskInfoMapper = sqlSession.getMapper(TaskInfoMapper.class);
        this.taskTagRuleMapper = sqlSession.getMapper(TaskTagRuleMapper.class);
    }

    //将所有查询Mysql的方法在这个类中集成
    public TagInfo getTagByTaskId(String taskId){

        validArgs(taskId);

        return tagInfoMapper.getTagByTaskId(taskId);

    }

    public TaskInfo getTaskById(String taskId){

        validArgs(taskId);

        return taskInfoMapper.getTaskById(taskId);

    }

    public List<TaskTagRule> getRulesByTaskId(String taskId){

        validArgs(taskId);

        return taskTagRuleMapper.getRulesByTaskId(taskId);

    }

    public List<TagInfo> getTagInfoTodayExecute(){
        return tagInfoMapper.getTagInfoTodayExecute();
    }

    public void validArgs(String...args){
        for (String arg : args) {
            //白字符: 参数要么是 空格，要么是回车，要么是\t，\n符号
            if (StringUtils.isBlank(arg)){
                throw new RuntimeException("参数非法!");
            }
        }
    }
}
